package com.ychs.servlet;

import com.ychs.entity.Competition;
import com.ychs.service.CompetitionService;
import com.ychs.service.IBaseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/22
 */
@WebServlet(value = "/CompetitionServlet")
public class CompetitionServlet extends BaseServlet {
    private IBaseService<Competition> service = new CompetitionService();
    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter competition add method");
        String category = request.getParameter("category");
        String year = request.getParameter("year");
        String title = request.getParameter("title");
        String leader = request.getParameter("leader");
        String members = request.getParameter("members");
        String level = request.getParameter("level");
        String teacher = request.getParameter("teacher");

        Competition competition = new Competition();
        competition.setCategory(category);
        competition.setYear(Integer.parseInt(year));
        competition.setTitle(title);
        competition.setLeader(leader);
        competition.setMembers(members);
        competition.setLevel(level);
        competition.setTeacher(teacher);

        boolean result = service.add(competition);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter competition modify method");
        String id = request.getParameter("id");
        String category = request.getParameter("category");
        String year = request.getParameter("year");
        String title = request.getParameter("title");
        String leader = request.getParameter("leader");
        String members = request.getParameter("members");
        String level = request.getParameter("level");
        String teacher = request.getParameter("teacher");

        Competition competition = new Competition();
        competition.setId(Integer.parseInt(id));
        competition.setCategory(category);
        competition.setYear(Integer.parseInt(year));
        competition.setTitle(title);
        competition.setLeader(leader);
        competition.setMembers(members);
        competition.setLevel(level);
        competition.setTeacher(teacher);

        boolean result = service.modify(competition);
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter competition remove method");
        String id = request.getParameter("id");
        boolean result = service.remove(Integer.parseInt(id));
        logger.info(result);
        String json = json(result);
        response.getWriter().write(json);
    }

    @Override
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter competition search method");
        Map<String, Object> conditions = new HashMap<>();

        String searchYear = request.getParameter("searchYear");
        if (searchYear != null && !"".equals(searchYear)) {
            conditions.put("year",searchYear);
        }

        int count = service.searchTotalNumber(conditions);

        String page = request.getParameter("page");
        String limit = request.getParameter("limit");

        if (page != null && limit != null) {
            int begin = (Integer.parseInt(page) - 1) * Integer.parseInt(limit);
            int pageSize = Integer.parseInt(limit);
            conditions.put("begin", begin);
            conditions.put("pageSize", pageSize);
        }else{
            conditions.put("begin", 0);
            conditions.put("pageSize", 10);
        }
        List<Competition> competitions = service.search(conditions);
        String json = json(count, competitions);
        logger.info(json);
        response.getWriter().write(json);
    }

    @Override
    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter competition searchById method");
        String id = request.getParameter("id");
        Competition competition = service.searchById(Integer.parseInt(id));
        logger.info(competition);
        String json = json(competition);
        response.getWriter().write(json);
    }
}
